home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / dns / tokenizer.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  309 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import cStringIO
  5. import sys
  6. import dns.exception as dns
  7. import dns.name as dns
  8. import dns.ttl as dns
  9. _DELIMITERS = {
  10.     ' ': True,
  11.     '\t': True,
  12.     '\n': True,
  13.     ';': True,
  14.     '(': True,
  15.     ')': True,
  16.     '"': True }
  17. _QUOTING_DELIMITERS = {
  18.     '"': True }
  19. EOF = 0
  20. EOL = 1
  21. WHITESPACE = 2
  22. IDENTIFIER = 3
  23. QUOTED_STRING = 4
  24. COMMENT = 5
  25. DELIMITER = 6
  26.  
  27. class UngetBufferFull(dns.exception.DNSException):
  28.     pass
  29.  
  30.  
  31. class Tokenizer(object):
  32.     
  33.     def __init__(self, f = sys.stdin, filename = None):
  34.         if isinstance(f, str):
  35.             f = cStringIO.StringIO(f)
  36.             if filename is None:
  37.                 filename = '<string>'
  38.             
  39.         elif filename is None:
  40.             if f is sys.stdin:
  41.                 filename = '<stdin>'
  42.             else:
  43.                 filename = '<file>'
  44.         
  45.         self.file = f
  46.         self.ungotten_char = None
  47.         self.ungotten_token = None
  48.         self.multiline = 0
  49.         self.quoting = False
  50.         self.eof = False
  51.         self.delimiters = _DELIMITERS
  52.         self.line_number = 1
  53.         self.filename = filename
  54.  
  55.     
  56.     def _get_char(self):
  57.         if self.ungotten_char is None:
  58.             if self.eof:
  59.                 c = ''
  60.             else:
  61.                 c = self.file.read(1)
  62.                 if c == '':
  63.                     self.eof = True
  64.                 elif c == '\n':
  65.                     self.line_number += 1
  66.                 
  67.         else:
  68.             c = self.ungotten_char
  69.             self.ungotten_char = None
  70.         return c
  71.  
  72.     
  73.     def where(self):
  74.         return (self.filename, self.line_number)
  75.  
  76.     
  77.     def _unget_char(self, c):
  78.         if self.ungotten_char is not None:
  79.             raise UngetBufferFull
  80.         
  81.         self.ungotten_char = c
  82.  
  83.     
  84.     def skip_whitespace(self):
  85.         skipped = 0
  86.         while True:
  87.             c = self._get_char()
  88.             if c != ' ' and c != '\t':
  89.                 if c != '\n' or not (self.multiline):
  90.                     self._unget_char(c)
  91.                     return skipped
  92.                 
  93.             
  94.             skipped += 1
  95.  
  96.     
  97.     def get(self, want_leading = False, want_comment = False):
  98.         if self.ungotten_token is not None:
  99.             token = self.ungotten_token
  100.             self.ungotten_token = None
  101.             if token[0] == WHITESPACE:
  102.                 if want_leading:
  103.                     return token
  104.                 
  105.             elif token[0] == COMMENT:
  106.                 if want_comment:
  107.                     return token
  108.                 
  109.             else:
  110.                 return token
  111.         
  112.         skipped = self.skip_whitespace()
  113.         if want_leading and skipped > 0:
  114.             return (WHITESPACE, ' ')
  115.         
  116.         token = ''
  117.         ttype = IDENTIFIER
  118.         while True:
  119.             c = self._get_char()
  120.             if c == '' or c in self.delimiters:
  121.                 if c == '' and self.quoting:
  122.                     raise dns.exception.UnexpectedEnd
  123.                 
  124.                 if token == '' and ttype != QUOTED_STRING:
  125.                     if c == '(':
  126.                         self.multiline += 1
  127.                         self.skip_whitespace()
  128.                         continue
  129.                     elif c == ')':
  130.                         if not self.multiline > 0:
  131.                             raise dns.exception.SyntaxError
  132.                         
  133.                         self.multiline -= 1
  134.                         self.skip_whitespace()
  135.                         continue
  136.                     elif c == '"':
  137.                         if not self.quoting:
  138.                             self.quoting = True
  139.                             self.delimiters = _QUOTING_DELIMITERS
  140.                             ttype = QUOTED_STRING
  141.                             continue
  142.                         else:
  143.                             self.quoting = False
  144.                             self.delimiters = _DELIMITERS
  145.                             self.skip_whitespace()
  146.                     elif c == '\n':
  147.                         return (EOL, '\n')
  148.                     elif c == ';':
  149.                         while None:
  150.                             c = self._get_char()
  151.                             if c == '\n' or c == '':
  152.                                 break
  153.                             
  154.                             token += c
  155.                             continue
  156.                             if want_comment:
  157.                                 self._unget_char(c)
  158.                                 return (COMMENT, token)
  159.                             elif c == '':
  160.                                 if self.multiline:
  161.                                     raise dns.exception.SyntaxError, 'unbalanced parentheses'
  162.                                 
  163.                                 return (EOF, '')
  164.                             elif self.multiline:
  165.                                 self.skip_whitespace()
  166.                                 token = ''
  167.                                 continue
  168.                             else:
  169.                                 return (EOL, '\n')
  170.                     want_comment
  171.                     token = c
  172.                     ttype = DELIMITER
  173.                 else:
  174.                     self._unget_char(c)
  175.                 break
  176.             elif self.quoting:
  177.                 if c == '\\':
  178.                     c = self._get_char()
  179.                     if c == '':
  180.                         raise dns.exception.UnexpectedEnd
  181.                     
  182.                     if c.isdigit():
  183.                         c2 = self._get_char()
  184.                         if c2 == '':
  185.                             raise dns.exception.UnexpectedEnd
  186.                         
  187.                         c3 = self._get_char()
  188.                         if c == '':
  189.                             raise dns.exception.UnexpectedEnd
  190.                         
  191.                         if not c2.isdigit() and c3.isdigit():
  192.                             raise dns.exception.SyntaxError
  193.                         
  194.                         c = chr(int(c) * 100 + int(c2) * 10 + int(c3))
  195.                     
  196.                 elif c == '\n':
  197.                     raise dns.exception.SyntaxError, 'newline in quoted string'
  198.                 
  199.             elif c == '\\':
  200.                 c = self._get_char()
  201.                 if c == '' or c not in self.delimiters:
  202.                     self._unget_char(c)
  203.                     c = '\\'
  204.                 
  205.             
  206.             token += c
  207.         if token == '' and ttype != QUOTED_STRING:
  208.             if self.multiline:
  209.                 raise dns.exception.SyntaxError, 'unbalanced parentheses'
  210.             
  211.             ttype = EOF
  212.         
  213.         return (ttype, token)
  214.  
  215.     
  216.     def unget(self, token):
  217.         if self.ungotten_token is not None:
  218.             raise UngetBufferFull
  219.         
  220.         self.ungotten_token = token
  221.  
  222.     
  223.     def next(self):
  224.         token = self.get()
  225.         if token[0] == EOF:
  226.             raise StopIteration
  227.         
  228.         return token
  229.  
  230.     
  231.     def __iter__(self):
  232.         return self
  233.  
  234.     
  235.     def get_int(self):
  236.         (ttype, value) = self.get()
  237.         if ttype != IDENTIFIER:
  238.             raise dns.exception.SyntaxError, 'expecting an identifier'
  239.         
  240.         if not value.isdigit():
  241.             raise dns.exception.SyntaxError, 'expecting an integer'
  242.         
  243.         return int(value)
  244.  
  245.     
  246.     def get_uint8(self):
  247.         value = self.get_int()
  248.         if value < 0 or value > 255:
  249.             raise dns.exception.SyntaxError, '%d is not an unsigned 8-bit integer' % value
  250.         
  251.         return value
  252.  
  253.     
  254.     def get_uint16(self):
  255.         value = self.get_int()
  256.         if value < 0 or value > 65535:
  257.             raise dns.exception.SyntaxError, '%d is not an unsigned 16-bit integer' % value
  258.         
  259.         return value
  260.  
  261.     
  262.     def get_uint32(self):
  263.         (ttype, value) = self.get()
  264.         if ttype != IDENTIFIER:
  265.             raise dns.exception.SyntaxError, 'expecting an identifier'
  266.         
  267.         if not value.isdigit():
  268.             raise dns.exception.SyntaxError, 'expecting an integer'
  269.         
  270.         value = long(value)
  271.         if value < 0 or value > 0x100000000L:
  272.             raise dns.exception.SyntaxError, '%d is not an unsigned 32-bit integer' % value
  273.         
  274.         return value
  275.  
  276.     
  277.     def get_string(self, origin = None):
  278.         (ttype, t) = self.get()
  279.         if ttype != IDENTIFIER and ttype != QUOTED_STRING:
  280.             raise dns.exception.SyntaxError, 'expecting a string'
  281.         
  282.         return t
  283.  
  284.     
  285.     def get_name(self, origin = None):
  286.         (ttype, t) = self.get()
  287.         if ttype != IDENTIFIER:
  288.             raise dns.exception.SyntaxError, 'expecting an identifier'
  289.         
  290.         return dns.name.from_text(t, origin)
  291.  
  292.     
  293.     def get_eol(self):
  294.         (ttype, t) = self.get()
  295.         if ttype != EOL and ttype != EOF:
  296.             raise dns.exception.SyntaxError, 'expected EOL or EOF, got %d "%s"' % (ttype, t)
  297.         
  298.         return t
  299.  
  300.     
  301.     def get_ttl(self):
  302.         (ttype, t) = self.get()
  303.         if ttype != IDENTIFIER:
  304.             raise dns.exception.SyntaxError, 'expecting an identifier'
  305.         
  306.         return dns.ttl.from_text(t)
  307.  
  308.  
  309.